package com.whnk.workflow.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.support.Convert;
import com.whnk.framework.util.ShiroUtils;
import com.whnk.system.domain.SysUser;
import com.whnk.system.mapper.SysUserMapper;
import com.whnk.workflow.domain.Leave;
import com.whnk.workflow.domain.LeaveApr;
import com.whnk.workflow.mapper.LeaveAprMapper;
import com.whnk.workflow.mapper.LeaveMapper;
import com.whnk.workflow.service.ILeaveAprService;
import com.whnk.workflow.utils.WorkflowUtils;

/**
 * 请假审批 服务层实现
 * 
 * @author whwhnk
 * @date 2019-04-16
 */
@Service
public class LeaveAprServiceImpl implements ILeaveAprService
{
	@Autowired
	private LeaveMapper leaveMapper;
	@Autowired
	private LeaveAprMapper leaveAprMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	
	/**
     * 查询请假审批信息
     * 
     * @param id 请假审批ID
     * @return 请假审批信息
     */
    @Override
	public LeaveApr selectLeaveAprById(Integer id)
	{
	    return leaveAprMapper.selectLeaveAprById(id);
	}
	
	/**
     * 查询请假审批列表
     * 
     * @param leaveApr 请假审批信息
     * @return 请假审批集合
     */
	@Override
	public List<LeaveApr> selectLeaveAprList(LeaveApr leaveApr)
	{
	    return leaveAprMapper.selectLeaveAprList(leaveApr);
	}
	
    /**
     * 新增请假审批
     * 
     * @param leaveApr 请假审批信息
     * @return 结果
     */
	@Override
	public int insertLeaveApr(LeaveApr leaveApr)
	{
	    return leaveAprMapper.insertLeaveApr(leaveApr);
	}
	
	/**
     * 修改请假审批
     * 
     * @param leaveApr 请假审批信息
     * @return 结果
     */
	@Override
	public int updateLeaveApr(LeaveApr leaveApr)
	{
	    return leaveAprMapper.updateLeaveApr(leaveApr);
	}
	
	/**
     * 删除请假审批对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteLeaveAprByIds(String ids)
	{
		return leaveAprMapper.deleteLeaveAprByIds(Convert.toStrArray(ids));
	}
	
	/**
	 * 审批
	 */
	@Override
	public int apr(String businessKey, String opinion, String approveReason) {
		SysUser sysUser = ShiroUtils.getSysUser();
		Leave leave = leaveMapper.selectLeaveById(businessKey);
		String step = leave.getStep();//当前节点
		Task task = WorkflowUtils.findTaskByBusinessKey(businessKey);//根据businessKey查询当前任务
		if (leave.getStep().equals("【部门经理】审批")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("branchManager", leave.getAprLoginName());//指定当前节点审批人
			map.put("bmStatus", opinion);//审批结果
			map.put("days", leave.getDays());//天数
			map.put("generalManager", "test");//指定下一节点审批人
			WorkflowUtils.complete(task.getId(), map);
			
			task = WorkflowUtils.findTaskByBusinessKey(businessKey);//根据businessKey查询当前任务
			if (task == null) {
				leave.setStep("归档");
				leave.setAprLoginName(leave.getLoginName());
				leave.setAprUserName(leave.getUserName());
			} else {
				SysUser su = sysUserMapper.selectUserByLoginName(task.getAssignee());//获取下一节点审批人
				leave.setStep(task.getName());
				leave.setAprLoginName(su.getLoginName());
				leave.setAprUserName(su.getUserName());
			}
			leave.setUpdateBy(sysUser.getUserId().toString());
			leaveMapper.updateLeave(leave);
		} else if (leave.getStep().equals("【总经理】审批")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("generalManager", leave.getAprLoginName());//指定当前节点审批人
			map.put("gmStatus", opinion);
			WorkflowUtils.complete(task.getId(), map);
			
			leave.setStep("归档");
			leave.setAprLoginName(leave.getLoginName());
			leave.setAprUserName(leave.getUserName());
			leave.setUpdateBy(sysUser.getUserId().toString());
			leaveMapper.updateLeave(leave);
		}
		
		LeaveApr leaveApr = new LeaveApr();
		leaveApr.setId(leaveAprMapper.selectNextId(businessKey));
		leaveApr.setBusinessKey(businessKey);
		leaveApr.setStep(step);
		leaveApr.setStatus(opinion);
		leaveApr.setReason(approveReason);
		leaveApr.setAprLoginName(sysUser.getLoginName());
		leaveApr.setAprUserName(sysUser.getUserName());
		return leaveAprMapper.insertLeaveApr(leaveApr);
	}
}
